package com.wxq.workpad.upgradation

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Build
import com.wxq.workpad.upgradation.DownloadDialog.DownloadListener
import com.wxq.workpad.upgradation.DownloadUtil.OnDownloadListener
import com.wxq.workpad.utils.ApplicationUtils
import com.wxq.workpad.utils.log.LogUtils
import java.io.File


// 升级管理单列类
object UpdateApkManager {

    // 测试下载地址 https://gulicourse.oss-cn-beijing.aliyuncs.com/app-release.apk

    val currentVersionCode=1

    /**
     * 获取当前app version code
     */
    fun getAppVersionCode(context: Context): Long {
        var appVersionCode: Long = 0
        try {
            val packageInfo: PackageInfo = context.getApplicationContext()
                .getPackageManager()
                .getPackageInfo(context.getPackageName(), 0)
            appVersionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                packageInfo.longVersionCode
            } else {
                packageInfo.versionCode.toLong()
            }
        } catch (e: PackageManager.NameNotFoundException) {
            LogUtils.e("appversioncode 获取失败")
        }
        return appVersionCode
    }

    /**
     * 获取当前app version name
     */
    fun getAppVersionName(context: Context): String? {
        var appVersionName = ""
        try {
            val packageInfo: PackageInfo = context.getApplicationContext()
                .getPackageManager()
                .getPackageInfo(context.getPackageName(), 0)
            appVersionName = packageInfo.versionName
        } catch (e: PackageManager.NameNotFoundException) {
          LogUtils.e("appversionname 获取失败")
        }
        return appVersionName
    }

  fun  downLoadApk(context: Context,pathUrl:String){
DownloadUtil.get().download("https://gulicourse.oss-cn-beijing.aliyuncs.com/app-release.apk",context.getExternalFilesDir("")!!.absolutePath+"/apk/","one.apk",object :OnDownloadListener{
    override fun onDownloadSuccess(file: File?) {
        LogUtils.e("下载陈宫")
    }

    override fun onDownloading(progress: Int) {
      LogUtils.e("当前下载进度"+progress)
    }

    override fun onDownloadFailed(e: Exception?) {
        LogUtils.e("下载失败")
    }

})


  }


    // 显示dialog
    fun showDialog(){
       var dialog= DownloadDialog(ApplicationUtils.getTopActivity())
        dialog.setListener(object :DownloadListener{
            override fun downloadApk() {

            }

            override fun isNeedDissmiss() {

            }

        })
        dialog.showDownloadDialog("最新内容", "")
    }
    fun showInsatllDialog(context: Context){
        var dialog= DownloadDialog(ApplicationUtils.getTopActivity())
        dialog.setListener(object :DownloadListener{
            override fun downloadApk() {

            }

            override fun isNeedDissmiss() {

            }

        })
        dialog.showInstallDialog("最新内容", context.getExternalFilesDir("")!!.absolutePath+"/apk/one.apk")
    }


}